﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace myCollection
{
    public partial class Dictionary : Form
    {

        public enum TypeD
        {
            MediaType,
            DataType,
            Category,
            Place
        }

        private TypeD type0;

        public Dictionary(TypeD d)
        {
            InitializeComponent();
            this.type0 = d;
            if (d == TypeD.MediaType || d == TypeD.DataType)
            {
                this.Text += d.ToString().Replace("Type", " Type");
            }
            else
            {
                this.Text += d.ToString();
            }
            this.refreshDGV();
        }

        private void refreshDGV(int rowIndex = 0)
        {
            dataGridView1.Rows.Clear();
            SQLite sqlite = new SQLite();
            sqlite.Connect();
            DataTable dt = new DataTable();
            dt = sqlite.GetDictionary(type0, tbSearch.Text);
            sqlite.Disconnect();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dataGridView1.Rows.Add(dt.Rows[i]["id"].ToString(), (i + 1).ToString(), dt.Rows[i]["name"].ToString(), dt.Rows[i]["count"].ToString());
            }
            if (rowIndex > 0 && dataGridView1.Rows.Count > 0 && rowIndex < dataGridView1.Rows.Count)
            {
                dataGridView1.Rows[rowIndex].Cells[1].Selected = true;
            }
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            new DictionaryAdd(type0).ShowDialog();
            this.refreshDGV(((dataGridView1.Rows.Count > 0) ? dataGridView1.CurrentCell.RowIndex : 0));
        }

        private void btDel_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentCell.RowIndex > -1)
            {
                if (MessageBox.Show("Delete: " + dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["name"].Value.ToString() + " ?", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
                {
                    SQLite sqlite = new SQLite();
                    sqlite.Connect();
                    sqlite.DeleteDictionary(type0, int.Parse(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["id"].Value.ToString()));
                    sqlite.Disconnect();
                    this.refreshDGV(((dataGridView1.Rows.Count > 0) ? dataGridView1.CurrentCell.RowIndex : 0));
                }
            }
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentCell.RowIndex > -1)
            {
                new DictionaryAdd(type0, int.Parse(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["id"].Value.ToString())).ShowDialog();
                this.refreshDGV(((dataGridView1.Rows.Count > 0) ? dataGridView1.CurrentCell.RowIndex : 0));
            }
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            this.refreshDGV();
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dataGridView1.CurrentCell.RowIndex > -1)
            {
                new DictionaryAdd(type0, int.Parse(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["id"].Value.ToString())).ShowDialog();
                this.refreshDGV(dataGridView1.CurrentCell.RowIndex);
            }
        }

        private void tbSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.refreshDGV();
            }
        }
    }
}
